﻿using System.Reflection;
using System.Web.Http;
using Abp.Application.Services;
using Abp.Configuration.Startup;
using Abp.Modules;
using Abp.WebApi;

namespace SingleUserMall.Api
{
    [DependsOn(typeof(AbpWebApiModule), typeof(SingleUserMallApplicationModule))]
    public class SingleUserMallWebApiModule : AbpModule
    {
        public override void PreInitialize()
        {
            Configuration.Modules.AbpWebCommon().SendAllExceptionsToClients = true;
            base.PreInitialize();
        }
        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());

            Configuration.Modules.AbpWebApi().DynamicApiControllerBuilder
                .ForAll<IAppService>(typeof(SingleUserMallApplicationModule).Assembly, "app")
                .ForMethods(i => { i.Verb = i.ActionName.StartsWith("Get") ? Abp.Web.HttpVerb.Get : Abp.Web.HttpVerb.Post; })
                .Build();
            Configuration.Modules.AbpWebApi().DynamicApiControllerBuilder
                .ForAll<IManageService>(typeof(SingleUserMallApplicationModule).Assembly, "manage")
                .ForMethods(i => { i.Verb = i.ActionName.StartsWith("Get") ? Abp.Web.HttpVerb.Get : Abp.Web.HttpVerb.Post; })
                .Build();

            Configuration.Modules.AbpWebApi().HttpConfiguration.Filters.Add(new HostAuthenticationFilter("Bearer"));

            //取消forgery验证
            Configuration.Modules.AbpWebApi().IsAutomaticAntiForgeryValidationEnabled = false;

            //配置跨域
            GlobalConfiguration.Configuration.EnableCors(new System.Web.Http.Cors.EnableCorsAttribute("*", "*", "*"));
        }
    }
}
